/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ps.packdescription.translation;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
import com.gargoylesoftware.htmlunit.JavaScriptPage;
import com.gargoylesoftware.htmlunit.WebClient;
import java.io.IOException;
import java.util.HashMap;

/**
 *
 * @author srkrishnan
 */
public class Translation {

    public static HashMap<String, HashMap<String,String>> translationmap = new HashMap<String, HashMap<String,String>>();
    public static String translate(String source, String fromLanguageCode, String toLanguageCode) {
        try {
            final WebClient webClient = new WebClient(BrowserVersion.getDefault());

            final JavaScriptPage page = webClient.getPage("http://translate.google.com/translate_a/t?client=t&text=" + source + "&hl=" + fromLanguageCode + "&sl=" + fromLanguageCode + "&tl=" + toLanguageCode + "&multires=1&ssel=0&tsel=0&sc=1");
            //assertEquals("HtmlUnit - Welcome to HtmlUnit", page.getTitleText());

            System.out.println(page.getContent());

            String response = page.getContent();

            webClient.closeAllWindows();

            return response.substring(response.indexOf("[\"") + 2, response.indexOf("\","));

        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (FailingHttpStatusCodeException ex) {
            ex.printStackTrace();
        }
        return null;

    }
}
